

function useWebsocket(url, handleMessage){
    const ws = new WebSocket(url)

    const init = ()=>{
        bindEvent()
    }

    function bindEvent(){
        ws.addEventListener('open',handleOpen, false)
        ws.addEventListener('close', handleClose, false)
        ws.addEventListener('error', handleError, false)
        ws.addEventListener('message', handleMessage,false)
    }

    function handleOpen(e){
        console.log("handleOpen", e)
    }

    function handleError(e){
        console.log("handleError",e)
    }

    function handleClose(e){
        console.log("handleClose",e)
    }

    init()
    // console.log(typeof ws)
    return ws

}

export default useWebsocket